﻿/*
    dotnetnuke.ajaxsearch.js for the Dot Net Nuke Ajax Search Module Copyright (C) 2011  Jaime Alberto 
    Jaramillo Zapata. (@g4m30n, peleleman@msn.com, jjaramillo@paradigmasolutions.com). This javascript
    function initializes a DotNetNuke Ajax Search Module Instance.
     
    This program is free software: you can redistribute it and/or modify it under the terms of the 
    GNU General Public License as published by the Free Software Foundation, version 3 of the License.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License  along with this program.  If not, see http://www.gnu.org/licenses/.
*/

jQuery.fn.InitializeAjaxSearch = function (options) {
    eval("var viewModel" + options.controlClientID + "; viewModel" + options.controlClientID + "= { resultCollection: ko.observableArray(), animateNewElement: function (elem) { $(elem).slideDown();  } }; ko.applyBindings(viewModel" + options.controlClientID + "); $('#" + options.searchButtonID + "').button().click( function () { if ($('#" + options.searchInputID + "').val() === 'Search me!') { $(document).ShowMessage( { messageType: 'warning', text: 'In order to search the website, you need to type something into the search box', title: 'Warning' } ); } else { var loadingoverlap = $('#" + options.searchContainerID + "').ShowLoadingOverlap(); viewModel" + options.controlClientID + ".resultCollection.remove(function (item) { return item != undefined }); AjaxSearch.Services.SearchService.GetSearchResults($('#" + options.searchInputID + "').val(), function (result) { if (result.length > 0) { for (var i = 0; i < result.length; i++) { viewModel" + options.controlClientID + ".resultCollection.push(result[i]); }  $('.searchresultlink').each( function () { $(this).button( { icons: { primary: 'ui-icon-link' } } ); } ); } else { $(document).ShowMessage( { messageType: 'information', text: 'No search results matching you criteria were found', title: 'We did not find anything' } ); } loadingoverlap[0].fadeOut('slow'); }, function (exception) { $(document).ShowMessage( { messageType: 'error', text: 'Sorry, seems like we are experiencing technical dificulties. We encourage to try again latter. If it does not work, contact the site administrators.<br/>Details: ' + exception.get_message(), title: 'Something went wrong' } ); loadingoverlap[0].fadeOut('slow'); } ); } } ); $('#" + options.searchInputID + "').hover( function () { $(this).addClass('ui-state-hover'); }, function () { $(this).removeClass('ui-state-hover'); } ).focusin( function () { $(this).addClass('ui-state-active'); if ($(this).val() === 'Search me!') { $(this).val(''); } } ).focusout( function () { $(this).removeClass('ui-state-active'); if ($(this).val() === '') { $(this).val('Search me!'); } } ); $('.searchresultlink').each( function () { $(this).button( { icons: { primary: 'ui-icon-link' } } ); } );");
};